package com.soda.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取要下载的课程ID
		String couvideo = request.getParameter("ci");

		// 乱码转换
		couvideo = new String(couvideo.getBytes("ISO8859-1"), "utf-8");

		InputStream in = this.getServletContext().getResourceAsStream(
				"\\video\\" + couvideo);
		// 先设置一个响应类型
		response.setContentType("application/x-msdownload");

		// 告诉浏览器 下载的文件名 (--处理下载文件名乱码--)
		couvideo = URLEncoder.encode(couvideo, "utf-8");
		response.addHeader("Content-Dispositon", "attachment;fileName=\""
				+ couvideo + "\"");
		ServletOutputStream ot = response.getOutputStream();
		byte bs[] = new byte[in.available()];
		in.read(bs);
		ot.write(bs);
		ot.flush();
		ot.close();
		in.close();

		// 将couIds转化成数组
		return;

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
